package day0218

import "sort"

type Pair struct {
	key   int
	value int
}

func topKFrequent(nums []int, k int) (res []int) {
	hmp := make(map[int]int)
	var t []Pair
	for _, v := range nums {
		hmp[v]++
	}
	for key, value := range hmp {
		t = append(t, Pair{key: key, value: value})
	}
	sort.Slice(t, func(i, j int) bool {
		if t[i].value >= t[j].value {
			return true
		}
		return false
	})
	for i := 0; i < k; i++ {
		res = append(res, t[i].key)
	}
	return
}
